﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Web;
using System.IO;
using Doo.Caching;
using Doo.Scripts;

namespace Doo.WebUI
{
    /// <summary>
    /// 扩展功能的页
    /// 1.支持资源合并[ResourceManager]
    /// 2.支持缓存
    /// </summary>
    public class DooPage : System.Web.UI.Page, IUseScriptObject
    {
        private ResourceManager res;
        public ResourceManager ResourceManager { get { return res; } }
        private WebManager web;
        public WebManager WebManager { get { return web; } }

        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);

            //资源管理器
            res = new ResourceManager(this);
            web = new WebManager(this);
        }

        /// <summary>
        /// 获取重写过程中的正则匹配
        /// </summary>
        public NameValueCollection RewriteMatch
        {
            get
            {
                return Context.Items[Doo.Config.DooKey.RewriterMatch] as NameValueCollection;
            }
        }

        public string ScriptObjectID
        {
            get;
            set;
        }

        private Controls.ScriptObject _sobject;
        public Controls.ScriptObject ScriptObject
        {
            get { return _sobject ?? (string.IsNullOrEmpty(ScriptObjectID) ? null : FindControl(ScriptObjectID) as Controls.ScriptObject); }
            set { _sobject = value; }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (!string.IsNullOrEmpty(ScriptBehind))
            {
                System.Web.UI.ScriptManager.GetCurrent(Page).Scripts.Add(new System.Web.UI.ScriptReference { Path = Page.ResolveUrl(ScriptBehind), ScriptMode = System.Web.UI.ScriptMode.Inherit });
            }
        }

        public string ScriptBehind { get; set; }
    }

}
